// pages/addPhone/addPhone.js
var util = require("../../utils/util.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    fromPage: "",
    flag: false,
    codeDis: false,
    phoneCode: "获取验证码",
    telephone: "",
    codePhone: "",
    hidden: true, //等待的展示与隐藏的控制
    buthidden: false //按钮的可用和不可用的控制
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '绑定手机号',
    })
    var that = this;
    // 获取用户信息
    wx.getStorage({
      key: 'userInfo',
      success: function (json) {
        that.setData({
          userInfo: json.data.userInfo
        })
        console.log(that.data.userInfo);
      },
    })
  },
  changeCode() {
    var _this = this
    let telephone = this.data.telephone
    if (telephone.length != 11 || isNaN(telephone)) {
      wx.showModal({
        title: '提示',
        content: "请输入有效的手机号码",
        showCancel: false
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
      return
    }
    this.setData({
      codeDis: true
    })
    //发送短信验证码
    wx.request({
      url: "https://boxapi.supergk.com/server/user/send_msg",
      data: util.json2Form({ tel: this.data.telephone }),
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        let data = res.data
        if (data.code == "0") {
          wx.showToast({
            title: "发送成功",
            icon: "success"
          })
          _this.setData({
            phoneCode: 60
          })
          let time = setInterval(() => {
            let phoneCode = _this.data.phoneCode
            phoneCode--
            _this.setData({
              phoneCode: phoneCode
            })
            if (phoneCode == 0) {
              clearInterval(time)
              _this.setData({
                phoneCode: "获取验证码",
                flag: true,
                codeDis: false
              })
            }
          }, 1000)
        } else {
          _this.setData({
            codeDis: false
          })
          wx.showModal({
            title: "提示",
            content: data.msg,
            showCancel: false
          })

          setTimeout(function () {
            wx.hideToast()
          }, 2000)
        }
      }
    })
  },
  phoneinput(e) {
    console.log(e)
    let value = e.detail.value
    console.log(value)
    this.setData({
      telephone: value
    })
  },
  codeinput(e) {
    let value = e.detail.value
    console.log(value)
    this.setData({
      codePhone: value
    })
  },

  // 点击提交手机号
  submitPhone: function () {
    var that = this;
    that.setData({
      buthidden: true
    })
    var uid = wx.getStorageSync("userInfo").userInfo.uid;
    var telephone = this.data.telephone;
    var codePhone = this.data.codePhone;
    
    if (telephone == '' || telephone.length != 11 || isNaN(telephone)) {
      wx.showModal({
        title: '提示',
        content: "手机号不正确",
        showCancel: false
      })
    } else if (codePhone == '') {
      wx.showModal({
        title: '提示',
        content: "请输入验证码",
        showCancel: false
      })
    } else if (uid && telephone && codePhone) {
      that.setData({
        buthidden: true
      })
      wx.request({
        url: 'https://boxapi.supergk.com/server/user/save_user_phone',
        // data: formData,
        data: util.json2Form({ uid: uid, phone: telephone, code: codePhone }),
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        method: "POST",
        success: function (res) {
          console.log(res.data)
          if (res.data.code == "0") {//绑定手机号成功
            that.setData({
              hidden: false,
              buthidden: true
            })
            console.log(res.data.msg);
            wx.showToast({
              title: '绑定成功',
              icon: "success",
            })
            setTimeout((function callback() {
              wx.navigateTo({
                url: '../index/index',
              })
            }).bind(this), 1000);
          } else {
            console.log(res.data.msg);
            that.setData({
              hidden: true,
              buthidden: false
            })
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
          }

        },
        fail: function (res) {
          that.setData({
            hidden: true,
            buthidden: false
          })
        },
        complete: function (res) { },
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})